package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.LineBorder;

import server.AdminServer;
import server.PlayerServer;
import server.Server;

public class LogInFrame extends JFrame {	
	
	private MainWindow mainFrame;
	private Server viewServer;
	
	private JPanel containerPanel;
	private JTextField userNameText;
	private JTextField passwordText;
	private Box buttonBox;
	private JButton logInButton;
	private JButton cancelButton;
	private final Color bkg = new Color(255,99,71);

	public LogInFrame(MainWindow m, Server server) {
		mainFrame = m;
		viewServer = server;
		
		setSize(300, 200);
		setTitle("Log In");
		containerPanel = new JPanel();
		containerPanel.setLayout(new GridLayout(0,1));
		containerPanel.setBorder(new LineBorder(bkg, 3));
		add(containerPanel);
		
		containerPanel.add(new JLabel("Enter Username:", SwingConstants.LEFT));
		userNameText = new JTextField();
		containerPanel.add(userNameText);
		containerPanel.add(new JLabel("Enter Password:"));
		passwordText = new JPasswordField();
		containerPanel.add(passwordText);
		
		buttonBox = new Box(BoxLayout.X_AXIS);
		containerPanel.add(buttonBox);
		logInButton = new JButton("Log In");
		logInButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				final String userName = userNameText.getText();
				final String password = passwordText.getText();
				SwingWorker<Server, Object> logger = new SwingWorker<Server, Object>(){
					protected Server doInBackground() throws Exception {
						AdminServer adminServer = viewServer.getAdminServer(userName, password);
						if(adminServer != null)
							return adminServer;
						PlayerServer playerServer = viewServer.getPlayerServer(userName, password);
						return playerServer;
					}
					protected void done(){
						try {
							Server server = get();
							if(server != null){
								if(server instanceof AdminServer){
									mainFrame.changeUser(server, UserType.ADMIN);
									SwingUtilities.getWindowAncestor(logInButton).dispose();
								} else{
									mainFrame.changeUser(server, UserType.USER);
									SwingUtilities.getWindowAncestor(logInButton).dispose();								
								}
							} else {
								JOptionPane.showMessageDialog(SwingUtilities.getRootPane(logInButton),
										"Username or password incorrect", "Error", JOptionPane.ERROR_MESSAGE);							
							}
						} catch (Exception e) {
							JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(logInButton),
									e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						}
					}
				};
				logger.execute();
			}
		});
		cancelButton = new CancelButton();
		buttonBox.add(logInButton);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(cancelButton);
		
		containerPanel.setBackground(bkg);
		setVisible(true);
	}
}
